package com.qfedu.openplateform.api.testApi.controller;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qfedu.openplateform.api.testApi.entity.Poem;
import com.qfedu.openplateform.common.constans.ExceptionDict;
import com.qfedu.openplateform.common.entity.ResponseData;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;

@RestController
@RequestMapping("/api/poem")
public class PoemController {

    @RequestMapping("/getPoem")
    public ResponseData getPoem() throws IOException {
        RestTemplate restTemplate = new RestTemplate();
        String url = "https://qqlykm.cn/api/yan/gc.php";

//        // 如果需要参数则使用 MultiValueMap 传递
//        MultiValueMap<String, Object> valueMap = new LinkedMultiValueMap<>();
//        valueMap.add("key","{'k1':'v1','k2':'v2','k3':'v3'}");

        String resultForPost = restTemplate.postForObject(url, null, String.class);

        String s = resultForPost.substring(resultForPost.indexOf("data") + 6, resultForPost.lastIndexOf("timestamp") - 2);

        Object o = JSON.parse(s);


        ObjectMapper mapper = new ObjectMapper();
        Poem poem = mapper.readValue(s, Poem.class);


        ResponseData<Object> responseData = new ResponseData<>();
        responseData.setCode(ExceptionDict.CONTENT_NULL_EXCEPTION);
        responseData.setMessage("生成古诗成功");
        responseData.setData(poem);


        return responseData;
    }
}
